const enterpriseVue = new Vue({
  el: "#app",
  data() {
    const Parent_Number = (rule, value, callback) => {
      if (this.form.Level != "1" && !value) {
        callback(new Error("当前菜单等级不是一级菜单，请选择父级菜单"));
      } else {
        callback();
      }
    };
    const Url = (rule, value, callback) => {
      if (this.children == "否" && !value) {
        callback(new Error("当前菜单不含子集，请输入菜单路由"));
      } else {
        callback();
      }
    };
    const Icon = (rule, value, callback) => {
      if (this.form.Level == "1" && !value) {
        callback(new Error("当前菜单是一级菜单，请选的菜单图标"));
      } else {
        callback();
      }
    };
    return {
      rules: {
        Model_Name: [
          { required: true, message: "请选择菜单名称", trigger: "blur" },
        ],
        Level: [{ required: true, message: "请选择菜单等级", trigger: "blur" }],
        Parent_Number: [
          { required: true, validator: Parent_Number, trigger: "change" },
        ],
        Url: [{ required: true, validator: Url, trigger: "blur" }],
        Icon: [{ required: true, validator: Icon, trigger: "change" }],
      },
      form: {
        Model_Number: "",
        Model_Name: "",
        Parent_Number: "",
        Level: "",
        Url: "",
        Path: "",
        Icon: "",
        Order: "",
        Children: {
          system_model_mark: [],
          system_model_list_detail: [
            {
              Mark_System_Number: "",
              Model_Number: "",
              System_SerialNumber: "",
            },
          ],
          // system_model_page: [{
          //   Page_SerialNumber: '',
          //   Model_Number: '',
          //   Page_Name: '',
          //   File_Name: '',
          //   Config_JSON: '',
          // }]
        },
      },
      Level: [
        {
          label: "一级菜单",
          value: "1",
        },
        {
          label: "二级菜单",
          value: "2",
        },
        {
          label: "三级菜单",
          value: "3",
        },
      ],
      Role: [
        {
          label: "全部",
          value: "0",
        },
        {
          label: "仅平台",
          value: "1",
        },
        {
          label: "仅用户",
          value: "2",
        },
      ],
      Icon: [],
      Parent: [],
      system_info: [],
      children: "否",
      loading: false,
      isParent: true,
      isUrl: false,
    };
  },
  mounted() {
    this.getIcon();
    this.getData();
  },
  methods: {
    getIcon() {
      const _this = this;
      $.ajax({
        url: absolutepath() + "utils/Icon.json",
        type: "GET",
        success: function (data) {
          _this.Icon = data;
        },
      });
    },
    getData() {
      const _this = this;
      $.ajax({
        // url: DomainName + "/getInquiryAction?token=" + $.cookie("token"),
        url: "https://service.kdddy.cn/donghui/generalBusiness/generalQueryByTableNameWithOriginal",
        type: "POST",
        data: {
          Data: "system_info",
          Condition: "Row_Status='0'",
        },
        success: function (res) {
          console.log(res);

          _this.system_info = res;
        },
        error: function () {
          // _this.$message.error("操作失败，请稍后再试");
        },
        // complete: function (XMLHttpRequest) {
        //   if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
        //     window.parent.document.location.href =
        //       absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
        //   }
        // },
      });
    },
    getMenu(e) {
      if (e != "1") {
        const _this = this;
        Level = e - 1 + "";
        $.ajax({
          url: DomainName + "/getLevelMenuAction?Level=" + Level,
          type: "GET",
          success: function (res) {
            _this.Parent = res;
            _this.isParent = false;
          },
          error: function () {
            // _this.$message.error("操作失败，请稍后再试");
          },
          // complete: function (XMLHttpRequest) {
          //   if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
          //     window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          //   }
          // }
        });
      } else {
        this.Parent = [];
        this.isParent = true;
        this.form.Parent_Number = "";
        this.children = "是";
        this.getUrl(this.children);
      }
    },
    getUrl(e) {
      if (e === "是") {
        this.isUrl = true;
        this.form.Url = "";
        this.form.Children.system_model_mark = [];
      } else if (e === "否") {
        this.isUrl = false;
        this.form.Children.system_model_mark = [];
      }
    },
    submitForm(formName) {
      console.log(this.form);

      return;
      let _this = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true;
          $.ajax({
            url: `${DomainName}/getAddAction?token=${$.cookie("token")}`,
            type: "POST",
            data: {
              Data: JSON.stringify({
                system_model_list: [_this.form],
              }),
              Operation_Name: "添加菜单",
              Type: "Add",
            },
            success: function (res) {
              if (res.Status === "success") {
                _this.$message.success("提交成功");
                window.history.go(-1);
              } else {
                _this.$message.error("提交失败，请稍后再试");
              }
            },
            error: function () {
              // _this.$message.error("操作失败，请稍后再试");
            },
            // complete: function (XMLHttpRequest) {
            //   _this.loading = false
            //   if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            //     window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
            //   }
            // }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    add_system_model_mark() {
      this.form.Children.system_model_mark.push({
        Mark_Number: "",
        Model_Number: "",
        Mark_Name: "",
      });
    },
    remove_system_model_mark() {
      if (this.form.Children.system_model_mark.length > 0) {
        this.form.Children.system_model_mark.pop();
      } else {
        this.$message({
          message: "不能在删除了",
          type: "warning",
        });
      }
    },
    add_system_model_list_detail() {
      this.form.Children.system_model_list_detail.push({
        Mark_System_Number: "",
        Model_Number: "",
        System_SerialNumber: "",
      });
    },
    remove_system_model_list_detail() {
      if (this.form.Children.system_model_list_detail.length > 1) {
        this.form.Children.system_model_list_detail.pop();
      } else {
        this.$message({
          message: "不能在删除了",
          type: "warning",
        });
      }
    },
    add_system_model_page() {
      this.form.Children.system_model_page.push({
        Page_SerialNumber: "",
        Model_Number: "",
        Page_Name: "",
        File_Name: "",
        Config_JSON: "",
      });
    },
    remove_system_model_page() {
      if (this.form.Children.system_model_page.length > 1) {
        this.form.Children.system_model_page.pop();
      } else {
        this.$message({
          message: "不能在删除了",
          type: "warning",
        });
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
});
